package org.example.backend.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.backend.dto.AnimalCategoryDTO;
import org.example.backend.dto.Response;
import org.example.backend.entity.AnimalCategory;
import org.example.backend.service.AnimalCategoryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "动物分类", description = "动物分类管理相关接口")
@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor
public class AnimalCategoryController {
    
    private final AnimalCategoryService categoryService;

    @Operation(summary = "创建分类")
    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Response<AnimalCategory> createCategory(@Valid @RequestBody AnimalCategoryDTO categoryDTO) {
        return Response.success(categoryService.createCategory(categoryDTO));
    }

    @Operation(summary = "更新分类")
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<AnimalCategory> updateCategory(
            @PathVariable Long id,
            @Valid @RequestBody AnimalCategoryDTO categoryDTO) {
        return Response.success(categoryService.updateCategory(id, categoryDTO));
    }

    @Operation(summary = "删除分类")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return Response.success();
    }

    @Operation(summary = "获取分类详情")
    @GetMapping("/{id}")
    public Response<AnimalCategory> getCategory(@PathVariable Long id) {
        return Response.success(categoryService.getCategory(id));
    }

    @Operation(summary = "获取分类列表")
    @GetMapping
    public Response<Page<AnimalCategory>> getCategories(Pageable pageable) {
        return Response.success(categoryService.getCategories(pageable));
    }

    @Operation(summary = "获取所有分类")
    @GetMapping("/all")
    public Response<List<AnimalCategory>> getAllCategories() {
        return Response.success(categoryService.getAllCategories());
    }
} 